package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

    //消费者没有service层
    //支持RestFul请求  RestTemplate。。。。封装好的 我们直接调用
    //参数(url,实体：Map,Class<T> responseType)
    @Autowired
    private RestTemplate restTemplate;

    //封装服务层的请求地址的前缀 不加ribbon
    //public static final String REST_URL_PREFIX = "http://localhost:8001";

    //使用Ribbon
    public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/{id}")
    public Dept queryById(@PathVariable("id")Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/"+id,Dept.class);
    }


    @RequestMapping("/consumer/dept/list")
    public List<Dept> queryAll(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }

    @RequestMapping("/consumer/dept/add")
    public Boolean addDept(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/addDept",dept,Boolean.class);
    }




}
